Passed
Push — development ( 30d901...c24fe4 )
by Peter
05:56 queued 13s
created

ZonesService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 84
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 8

4 Functions

Rating   Name   Duplication   Size   Complexity  
A findByCity 0 7 1
B getZonesByFilter 0 51 5
A pointInParkingZone 0 7 1
A findAll 0 4 1
1 5
import { Injectable } from '@nestjs/common';
2 5
import { InjectRepository } from '@nestjs/typeorm';
3 5
import { Repository } from 'typeorm';
4 5
import { Zone } from './entities/zone';
5
import { ZoneQuery } from './types/ZoneQuery';
6
import { ZoneResponse } from './types/ZoneResponse';
7 5
import { BicyclesService } from 'src/bicycles/bicycles.service';
8 5
import { getDistance, positionInsidePolygon } from 'src/utils/geo.utils';
9
import { CityName } from 'src/cities/types/city.enum';
10
11
@Injectable()
12 5
export class ZonesService {
13
  constructor(
14
    @InjectRepository(Zone)
15 10
    private readonly zoneRepository: Repository<Zone>,
16 10
    private readonly bicyclesService: BicyclesService,
17
  ) { }
0 ignored issues
show
introduced by
Delete ·
Loading history...
18
19
  async findAll(): Promise<Zone[]> {
20 8
    return await this.zoneRepository.find({
21
      relations: ['speedZone', 'city'],
22
    });
23
  }
24
25
  async findByCity(cityName: CityName): Promise<Zone[]> {
26 2
    return await this.zoneRepository.find({
27
      relations: ['city'],
28
      where: {
29
        city: {
30
          name: cityName,
31
        },
32
      }
0 ignored issues
show
introduced by
Insert ,
Loading history...
33
    });
34
  }
35
  
0 ignored issues
show
introduced by
Delete ··
Loading history...
36
  // seems unused
37
  // async getZones(lat: number, lon: number): Promise<Zone[]> {
38
  //   let zones = await this.findAll();
39
  //   zones = zones.filter((zone) => {
40
  //     return positionInsidePolygon(lat, lon, zone.polygon);
41
  //   });
42
  //   return zones;
43
  // }
44
45
  async getZonesByFilter(query: ZoneQuery): Promise<ZoneResponse> {
46 5
    const zones: ZoneResponse = {
47
      zones: [],
48
    };
49 5
    zones.zones = await this.findAll();
50
51 5
    if (query.city && query.city.length > 0) {
52 2
      zones.zones = zones.zones.filter((zone) => {
53 6
        return query.city.includes(zone.city.name);
54
      });
55
    }
56
57 5
    if (query.type && query.type.length > 0) {
58 2
      zones.zones = zones.zones.filter((zone) => {
59 5
        return query.type.includes(zone.type);
60
      });
61
    }
62
63 5
    if (query.lat && query.lon) {
64 1
      console.log(query.lat, query.lon);
65 1
      zones.zones = zones.zones.filter((zone) => {
66 3
        return (
67
          getDistance(query.lat, query.lon, zone.polygon[0].lat, zone.polygon[0].lng) <= query.rad
68
        );
69
      });
70
    }
71
72 5
    if (query.includes && query.includes.length > 0) {
73 2
      const allBikes = await this.bicyclesService.findAll();
74 2
      zones.zones = zones.zones.map((zone) => {
75 4
        const bikes = allBikes.filter((bike) => {
76 8
          return positionInsidePolygon(bike.latitude, bike.longitude, zone.polygon);
77
        });
78 4
        return {
79
          ...zone,
80
          bikes: bikes,
81
        };
82
      });
83
    }
84
85 5
    return zones;
86
  }
87
88
  async pointInParkingZone(lat: number, lon: number): Promise<boolean> {
89 2
    const zones = (await this.findAll()).filter((zone) => {
90 6
      return zone.type === 'parking';
91
    });
92 2
    return zones.some((zone) => {
93 3
      return positionInsidePolygon(lat, lon, zone.polygon);
94
    });
95
  }
96
}
97